package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallStorageMapper;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.domain.LitemallStorageExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @date 2019/7/20 -21:44
 */
@Service
public class LitemallStorageService {

    @Resource
    private LitemallStorageMapper storageMapper;

    public void add(LitemallStorage storageInfo) {
        storageInfo.setAddTime(new Date());
        storageInfo.setUpdateTime(new Date());
        storageMapper.insertSelective(storageInfo);
    }

    public boolean deleteByKey(String key){
        return storageMapper.deleteByKey(key) > 0 ? true : false;
    }

    public boolean update(LitemallStorage storage){
        storage.setUpdateTime(new Date());
        if(storageMapper.updateByPrimaryKeySelective(storage)>0){
            return true;
        }
        return false;
    }

    public LitemallStorage findById(Integer id){
        return storageMapper.selectByPrimaryKey(id);
    }

    public List<LitemallStorage> querySelective(String key,String name,Integer page,Integer limit,String sort,String order){
        LitemallStorageExample storageExample = new LitemallStorageExample();
        LitemallStorageExample.Criteria criteria = storageExample.createCriteria();

        if(!StringUtils.isEmpty(key)){
            criteria.andKeyLike("%" + key + "%");
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if(!StringUtils.isEmpty(sort) || !StringUtils.isEmpty(order)){
            storageExample.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page,limit);
        return storageMapper.selectByExample(storageExample);
    }

}
